﻿Imports System.Data.SqlClient
Imports CodeMatrix.Context
Imports System.Linq

Partial Class Apps_Shares_Uc_stp_auth_staff_list
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        'Hide column checkbox
        Dim btnDelete As Button = CType(gvDisplay.FindControl("btnDelete"), Button)
        If Not btnDelete Is Nothing Then
            btnDelete.Visible = Not String.IsNullOrEmpty(Request.QueryString("del"))
        End If
        ' Set breadcrump
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        If Not Request.QueryString("del") Is Nothing Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 44)
        ElseIf Not Request.QueryString("edit") Is Nothing Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 43)
        Else
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 41)
        End If
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnDelete.Click
        Try
            gvDisplaySrc.Delete()
        Catch ex As SqlException
            GenericHelper.ShowAlert(Me, String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, ""))
        End Try
    End Sub

    Protected Sub gvDisplaySrc_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles gvDisplaySrc.Deleting

        Try
            ' ============================================
            ' Assign parameter to SQL statement to delete
            ' ============================================
            Dim keys As Int32() = CodeMatrix.Utilities.WebUiHelper.GetSelectedCheckBox(Of Int32)(gvDisplay, "staff_code", "icbItem")
            Dim ctx As DataClassesDataContext = New DataClassesDataContext
            Try
                
            Catch ex As Exception
                e.Cancel = True
                GenericHelper.ShowAlert(Me, ex.Message)
            End Try

            If keys.Length > 0 Then
                ' Modified by Boonyarit Yingvoragan
                ' Modified date 29/10/2553
                e.InputParameters("cid") = CodeMatrix.Utilities.CodeHelper.CreateSeriesOfParameters(keys)
            Else
                e.Cancel = True
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub gvDisplaySrc_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles gvDisplaySrc.Selected

        Try
            ' Modified by Boonyarit Yingvoragan
            ' Modified date 31/10/2553
            ' Retrieve the AffectedRows value from return value
            If Not e.ReturnValue Is Nothing Then
                Dim ReturnValue As System.Data.DataTable = e.ReturnValue
                e.AffectedRows = ReturnValue.Rows.Count

                If e.AffectedRows > 0 Then
                    ltrCountView.Text = String.Format("จำนวน {0} เร็คคอร์ด", e.AffectedRows)
                Else
                    ltrCountView.Text = String.Empty
                End If
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub gvDisplaySrc_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles gvDisplaySrc.Selecting

        Try
            If Not Page.IsPostBack Then
                e.Cancel = True
            Else
                e.InputParameters("department_code") = ddlDepartment.SelectedValue
                e.InputParameters("group_code") = ddlGroupUser.SelectedValue
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click

        Try
            gvDisplay.DataBind()
            gvDisplay.Sort("staff_firstname", SortDirection.Ascending)
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub gvDisplay_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles gvDisplay.Sorting

        Try
            If (String.IsNullOrEmpty(e.SortExpression)) Then
                Dim oldSortCol As Image = gvDisplay.FindControl(gvDisplay.SortExpression)
                If Not oldSortCol Is Nothing Then oldSortCol.Visible = False
            Else
                Dim oldSortCol As Image = gvDisplay.FindControl(gvDisplay.SortExpression)
                Dim newSortCol As Image = gvDisplay.FindControl(e.SortExpression)

                If (Not oldSortCol Is Nothing) Then oldSortCol.Visible = False
                If (Not newSortCol Is Nothing) Then
                    newSortCol.Visible = True
                    If e.SortDirection = SortDirection.Ascending Then
                        newSortCol.ImageUrl = "../images/btn_down.png"
                    Else
                        newSortCol.ImageUrl = "../images/btn_up.png"
                    End If
                End If
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        ddlGroupUser.SelectedIndex = -1
    End Sub

    Protected Sub ddlGroupUser_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroupUser.SelectedIndexChanged
        ddlStaff.SelectedIndex = -1
    End Sub
End Class
